package com.lagou.server.monitor.tp;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @author caiwei
 * @date 2020-11-6 0:00
 */
@Slf4j
@Activate(group = {CommonConstants.PROVIDER,CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {

    private final TPMonitor tpMoniter = new TPMonitor();

    public TPMonitorFilter() {
       new Thread(tpMoniter).start();
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long now = System.currentTimeMillis();
        Result result = invoker.invoke(invocation);
        long useTime = System.currentTimeMillis() - now;
        tpMoniter.request(System.currentTimeMillis(),useTime);
        return result;
    }
}
